package com.bone.spider.utils;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author learnting
 * @date 2020-03-16
 */
public final class UrlUtils {

  public static String getParam(String url, String name) {
    String params = url.substring(url.indexOf("?") + 1);
    return Arrays.stream(params.split("&")).filter(param -> param.startsWith(name))
      .findFirst().map(param -> param.substring(param.indexOf('=') + 1)).orElse("");
  }

  public static Map<String, String> getParams(String url) {
    String params = url.substring(url.indexOf("?") + 1);
    return Arrays.stream(params.split("&")).collect(
      Collectors.toMap(param -> param.substring(0, param.indexOf('=')),
        param -> param.substring(param.indexOf('=') + 1)));
  }
}
